package com.ld.security.auth.shiro.login.pass;


import com.ld.security.auth.common.sys.entity.SysPerEntity;
import com.ld.security.auth.common.sys.entity.SysRoleEntity;
import com.ld.security.auth.common.sys.entity.SysUserEntity;
import com.ld.security.auth.common.sys.login.UserService;
import com.ld.security.auth.common.sys.vo.UserDetailsVo;
import com.ld.security.auth.common.sys.vo.UserInfoVo;
import com.ld.security.auth.shiro.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

@Slf4j
public class PasswordRealm extends AuthorizingRealm {


    private UserService userAuthService;

    public UserService getUserAuthService() {
        return userAuthService;
    }

    public void setUserAuthService(UserService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        if(token instanceof PasswordToken){
            return true;
    }

        return false;
    }

    /**
     * 授权认证,设置角色/权限信息
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();

        log.debug("doGetAuthorizationInfo principalCollection...");

        // 设置角色/权限信息
        String token = principalCollection.toString();
        // 获取username
        String username = JwtUtil.getUsername(token);

        UserDetailsVo detailsVo =  userAuthService.getUserDetails(username);

        for (SysRoleEntity sysRoleEntity : detailsVo.getRoles()) {
            info.addRole(sysRoleEntity.getCode());
        }
        for (SysPerEntity sysPerEntity : detailsVo.getPers()) {
            info.addStringPermission(sysPerEntity.getName());
        }
        return  info;
    }


    /**
     * 登陆认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.debug("doGetAuthenticationInfo authenticationToken...");
        // 校验token

        PasswordToken token = (PasswordToken)authenticationToken;
        String username = token.getPrincipal().toString();

        UserInfoVo userInfoVo =  userAuthService.getUserInfo(username);
        SysUserEntity userEntity =  userInfoVo.getUser();

        if(userEntity == null){
            throw new AuthenticationException("用户不存在");
        }

        return new SimpleAuthenticationInfo(
                userEntity.getUsername(),
                userEntity.getPassword(),
                getName()
        );

    }
}
